package csc.daonjpa.java.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import share.Constant;
import csc.daonjpa.java.dao.TransactionDAO;
import csc.daonjpa.java.dao.TransactionStateDAO;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.Transaction;
import csc.daonjpa.java.domain.TransactionState;

@Service
public class TransactionService {
	@Autowired
	private TransactionStateDAO transactionStateDAO;
	
	@Autowired
	private TransactionDAO transactionDAO;
	/**
	 * @return the transactionStateDAO
	 */
	public TransactionStateDAO getTransactionStateDAO() {
		return transactionStateDAO;
	}

	/**
	 * @param transactionStateDAO the transactionStateDAO to set
	 */
	public void setTransactionStateDAO(TransactionStateDAO transactionStateDAO) {
		this.transactionStateDAO = transactionStateDAO;
	}

	public TransactionState getTransactionState(Integer transactionId) {
		return transactionStateDAO.getTransactionStateById(transactionId);
	}
	
	public List<Transaction> getTransferedTransaction(Long customerLoginId){
		return transactionDAO.getTransferedTransactionByCustomerId(customerLoginId);
	}
	
	public List<Transaction> getReceivedTransaction(Long customerLoginId){
		return transactionDAO.getReceivedTransactionByCustomerId(customerLoginId);
	}

	public List<Transaction> getTransactions(Long customerId, String date) {
		return transactionDAO.getTransactions(customerId, date);
	}

	public boolean addTransaction(Transaction transaction) {
		return transactionDAO.addTransaction(transaction);
	}
	
	public List<Customer> getReceivedCustomer(Long customerId){
		return transactionDAO.getCustomers(customerId);
	}

	public List<Transaction> getAllTransactionList() {
		return transactionDAO.getAllTransactionList();
	}

	public Transaction getTransactionDetailById(Long transactionId) {
		return transactionDAO.getTransactionDetails(transactionId);
	}

	public void setApproveTransactionById(Long transactionId) {
		Transaction transaction = getTransactionDetailById(transactionId);
		long amount = transaction.getAmount();
		Customer receiver = transaction.getReceivedCustomer();
		//we then add the transfer amount to receiver amount
		receiver.setAmount(receiver.getAmount() + amount);
		transactionDAO.setApproveTransactionById(transaction);
	}

	public void setDenyTransactionById(Long transactionId, String reason) {
		Transaction transaction = getTransactionDetailById(transactionId);
		Customer sendCustomer = transaction.getSendCustomer();
		long amount = transaction.getAmount();
		//if denied, we return the transfered amount to it sender
		sendCustomer.setAmount(sendCustomer.getAmount() + amount);
		transactionDAO.setDenyTransactionById(transaction, reason);
	}
	
	public boolean isTransferable(Long customerId, String amount, String date){
		//khoi tao bien boolean co the chuyen tien duoc 'transferable' = false
		boolean transferable = false;
		
		//lay danh sach transaction lstTransaction cua 'customerId' trong ngay 'date'
		List<Transaction> lstTransaction = getTransactions(customerId, date);
		
		long amountSum = Long.parseLong(amount);
		
		
		if (null != lstTransaction) {
			for(Transaction trans : lstTransaction){
				amountSum = amountSum + trans.getAmount();
			}
		}
		
		//neu so luong phan tu trong danh sach la 3, hoac tong so tien chuyen trong ngay + 'amount' > 30.000.000
		if ((null == lstTransaction || lstTransaction.size() < Constant.MAXIMUM_TRANSFER_PER_DAY) && amountSum <= Constant.MAXIMUM_TRANSFER_AMOUNT_PER_DAY) {
			transferable = true;
		}
		else{
			transferable = false;
		}
		
		return transferable;
	}
	
	/**
	 * This method caculate the amount for sendCustomer and receivedCustomer IF AND ONLY IF TRANSACTION IS
	 * AUTOMATICALLY APPROVED
	 * @param sendCustomer
	 * @param receiveCustomer
	 * @param amount
	 */
	public void recalculateAmount(Customer sendCustomer, Customer receiveCustomer, String amount) {
		Long lngAmount = Long.valueOf(amount);
		sendCustomer.setAmount(sendCustomer.getAmount() - lngAmount);
		receiveCustomer.setAmount(receiveCustomer.getAmount() + lngAmount);
	}
}
